import TableData from "@/components/public/el-table"; //主页面列表
import TableLyout from '@/components/commonPublic/tableLyout.vue'

import CustomDialog from "@/components/reboundStrengthManage/reboundDetectionData/index.vue";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置
import CustomDialogs from "@/components/reboundStrengthManage/reboundDetectionTask/index.vue";
import messageBox from "@/components/public/messageBox"; //主页面分页
import actions from "@/actions";
import API from '@/utils/commonRequest.js';
import {
    getConcreteCheckSamplePageList, //列表
    submitConcreteCheckSample, //提交
    revokeConcreteCheckSample, //撤销
} from "@/api/request.js";
export default {
    name: "reboundDetectionEquipment",
    data() {
        return {
            name: "reboundDetectionEquipment",
            userNature: null, //用户Nature
            showMsgBox: false,
            msgTip: "请先选中数据行！",
            msgType: 1,
            isLock: false,
            loading: false,
            btnObj: formJs.btnObj, //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            // 列表
            getList: {
                fprojectid: "",
                startDates: "", // 开始时间
                endDates: "", // 截至时间
                modelType: "3", // 0待办 1已办 2查询
                userId: "", // 查询待办或已办时传 列表页查询不传
                fCompanyid: "",
                page: 1, // 当前页码
                pageSize: 15, // 每页显示条数
                timeDates: null,
                keyType: ""
            },
            isShow: false, //是否显示弹框
            isShows: false, //是否显示弹框
            statusNum: null, //点击那个按钮
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            tableData: [],
            dialogTitle: null,
        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        messageBox,

        CustomDialog, //报告记录编号
        CustomDialogs //任务编号
    },
    mounted() {
        API.userNature().then((res) => {
            if (res.errcode == 0) {
                this.userNature = res.data.Nature;
            }
        }).catch((err) => {
            console.log("API", err)
        })
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser
        }, true);
        this.getDate()
        this.table_config.operation.cols = [{
            prop: "fStatus",
            type: "primary",
            handleRow: this.handleRow,
        },];
        this.table_config.operation["specialClass"] = function (rowData) {
            var color = null;
            if (rowData == "待检测") {
                color = "red !important";
            } else if (rowData == "检测中") {
                color = "orange !important";
            } else if (rowData == "已完成") {
                color = "green !important";
            } else {
                color = "#409eff !important";
            }
            return color;
        };
    },
    methods: {
        confirmMsg() {
            this.showMsgBox = false;
        },
        refreshList() { //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        async getTender(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                headers: {
                    "Content-Type": "application/json",
                    Authorization: localStorage.getItem("access_token"),
                },
            }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    this.tendersObj = [];
                    if (data.length == 0) {
                        this.$message.warning({
                            content: "有其他人员登录此账号！",
                            duration: 1000,
                        });
                        setTimeout(() => {
                            window.location.href = "http://172.18.5.201:1080/login";
                        }, 1200);
                    } else  if (data.length == 1) {
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                        this.getList.fCompanyid = data[0].sectionId;
                    } else {
                        this.tendersObj = [{
                            fItemName: "所有标段",
                            fItemValue: "",
                        },];
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                    }
                }
            });
        },
        closeDialog() { //报告记录编号
            this.isShow = false;
        },
        closeDialogs() { //任务编号
            this.isShows = false;
        },

        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },
        // 新增/编辑
        yj_edit(num) {
            // var data;
            if (num == 1) { //编辑
                if (this.dataFid != null) {
                    if (this.currentRow.fStatus == "待检测" || this.currentRow.fStatus == "检测中") {
                        this.editText();
                    } else {
                        this.$message.warning({
                            content: "请选择信息状态为'待检测'或'检测中'的数据！",
                            duration: 1000
                        })
                    }
                } else {
                    this.$message.warning({
                        content: "请先选择需要编辑的数据",
                        duration: 1000
                    })

                }

            } else if (num == 2) { //提交
                if (this.dataFid != null) {
                    // if (this.currentRow.fCheckusername != "") {
                    this.yj_submit(this.currentRow);
                    // } else {
                    //     this.$message.warning({
                    //         content: "复核人不能为空",
                    //         duration: 1000
                    //     })
                    // }
                } else {
                    this.$message.warning({
                        content: "请先选择需要提交的数据",
                        duration: 1000
                    })

                }
            } else if (num == 4) { //撤销
                if (this.userNature == '施工单位'||this.userNature == '中心试验室') {
                    if (this.dataFid != null) {

                        if (this.currentRow.fStatus == "待检测") {
                            this.yj_revoke();
                        } else {
                            this.$message.warning({
                                content: "请选择检测状态为'待检测'的数据！",
                                duration: 1000
                            })
                        }

                    } else {
                        this.$message.warning({
                            content: "请先选择需要撤销的数据！",
                            duration: 1000
                        })
                    }
                } else {
                    this.$message.warning({
                        content: "该用户无权限",
                        duration: 1000
                    })
                }
            } else if (num == 7) { //查看
                if (this.dataFid != null) {
                    this.statusNum = 7
                    this.isShow = true;
                    this.editText();
                } else {
                    this.$message.warning({
                        content: "请先选择需要查看的数据",
                        duration: 1000
                    })


                }
            } else if (num == 5) {
                if (this.dataFid != null) {
                    if (this.currentRow.fStatus == "已完成") {
                        window.open(" http://172.18.5.31:8300/IOTManagement/Web/Check/getExcelByConcreteSample.do?fId=" + this.dataFid);
                    } else {
                        this.$message.warning({
                            content: "请先选择状态为“已完成”的数据",
                            duration: 1000
                        })
                    }
                } else {

                    this.$message.warning({
                        content: "请先选择需要打印的数据",
                        duration: 1000
                    })

                }
            } else if (num == 6) {
                if (this.dataFid != null) {
                    if (this.currentRow.fStatus == "已完成") {
                        window.open(" http://172.18.5.31:8300/IOTManagement/Web/Check/getExcelByConcreteSampleCode.do?fId=" + this.dataFid);
                    } else {
                        this.$message.warning({
                            content: "请先选择状态为“已完成”的数据",
                            duration: 1000
                        })

                    }

                } else {
                    this.$message.warning({
                        content: "请先选择需要打印的数据",
                        duration: 1000
                    })


                }
            }

        },
        editText() {
            this.isShow = true;
        },

        // 查看
        // 点击查看
        yj_viewForm(row, label) {
            console.log(row, label);
            if (label == "报告编号") {
                this.currentRow = row;
                this.statusNum = 7
                this.isShow = true;

            } else if (label == "记录编号") {
                this.currentRow = row;
                this.statusNum = 8
                this.isShow = true;
            } else if (label == "任务编号") {
                this.currentRow = row;
                // this.statusNum = 7
                // this.isShows = true;
                this.currentRow = row;
                this.$router.push({
                    path: "/reboundStrengthManage/reboundDetectionTaskText",
                });
                var data = {
                    data: this.currentRow,
                    fCompanyid: this.getList.fCompanyid,
                    status: 7,
                    getList: this.getList,

                }
                sessionStorage.setItem(
                    "GxEmbedded_primaryInfo",
                    JSON.stringify(data)
                );
            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
        },

        // 获取列表数据
        yj_searchForm(data) {
            window.sessionStorage.setItem(
                "GxEmbedded-reboundStrengthManage_getList",
                JSON.stringify(data)
            );
            var id;
            if (data.fCompanyid != "") {
                id = data.fCompanyid;
            } else {
                id = data.fprojectid;
            }
            var obj = {
                startDates: data.startDates, // 开始时间
                endDates: data.endDates, // 截至时间
                modelType: 2, // 0待办 1已办 2查询
                userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
                userId: this.userInfo.id, // 查询待办或已办时传 列表页查询不传
                fCompanyid: id,
                pageSize: data.pageSize, // 每页显示条数
                keyType: data.keyType
            }
            this.loading = true;
            getConcreteCheckSamplePageList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var that = this;
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        that.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表", res.msg);
                        this.loading = false;
                    }
                })
                .catch(() => {
                    this.loading = false;
                    console.log("获取列表数据接口报错");
                });
        },
        // 撤销
        yj_revoke() {
            if (this.currentRow != null) {
                if (this.dataFid != "" && this.dataFid != null) {

                    this.isLock = true
                    this.loading = true;
                    revokeConcreteCheckSample({
                        fId: this.dataFid,
                        userId: this.userInfo.userId, //当前登录人id
                        userName: this.userInfo.realName //当前登录人姓名
                    })
                        .then((res) => {
                            if (res.errcode == 0) {
                                this.loading = false;
                                this.dataFid = null
                                this.isLock = false
                                this.yj_searchForm(this.getList);
                                this.$message.success({
                                    content: "撤销成功",
                                    duration: 1000
                                })

                            } else if (res.errcode == -2) {
                                this.loading = false;
                                this.dataFid = null
                                this.isLock = false
                                this.yj_searchForm(this.getList);
                                this.$message.warning({
                                    content: res.msg,
                                    duration: 1000
                                })
                            } else {
                                this.$message.error({
                                    content: "撤销失败" + res.msg,
                                    duration: 1000
                                })
                                this.isLock = false
                                this.loading = false;
                                return false;
                            }
                        })
                        .catch(() => {
                            this.loading = false;
                            console.log("撤销失败");
                        });


                } else {
                    this.$message.warning({
                        content: "请先选择需要撤销的数据",
                        duration: 1000
                    })
                }
            }
        },
        // 提交
        yj_submit(row) {
            if (row.fId != "" && row.fId != null && row.fId != undefined) {
                if (row.fStatus == "检测中") {
                    this.loading = true;
                    submitConcreteCheckSample({
                        fId: row.fId,
                        userId: this.userInfo.userId, //当前登录人id
                        userName: this.userInfo.realName, //当前登录人姓名
                        userAccount: this.userInfo.loginName,
                    })
                        .then((res) => {
                            if (res.errcode == 0) {
                                this.loading = false;
                                this.yj_searchForm(this.getList);
                                this.$message.success({
                                    content: "提交成功",
                                    duration: 1000
                                })
                            } else {
                                this.$message.error({
                                    content: "提交失败",
                                    duration: 1000
                                })
                                this.loading = false;
                            }
                        })
                        .catch(() => {
                            console.log("提交失败");
                            this.loading = false;
                        });
                } else {
                    this.$message.warning({
                        content: '请选择信息状态为"编辑中"的数据！',
                        duration: 1000
                    })
                }
            } else {
                this.$message.warning({
                    content: "请先选择需要提交的数据",
                    duration: 1000
                })
            }
        },
        selectTime(e) {
            if (e[0] != "" && e[1] != "") {
                this.getList.startDates = e[0]; // 开始时间
                this.getList.endDates = e[1]; // 截至时间
            } else {
                this.getList.startDates = ""; // 开始时间
                this.getList.endDates = ""; // 截至时间
            }
            this.getList.timeDates = [e[0], e[1]]
            this.yj_searchForm(this.getList);
        },
        getDate() {



            if (JSON.parse(sessionStorage.getItem("GxEmbedded-reboundStrengthManage_getList"))) {
                var data = JSON.parse(sessionStorage.getItem("GxEmbedded-reboundStrengthManage_getList"));
                if (data.fprojectid != "") {
                    this.getTender(data.fprojectid);
                } else {
                    data.fprojectid = this.projectsObj[1].fItemValue;
                    this.getTender(data.fprojectid);
                }
                this.getList = {
                    fprojectid: data.fprojectid,
                    fCompanyid: data.fCompanyid, //左侧选中组织机构id
                    keyType: "",
                    page: 1, //当前页码
                    pageSize: 15, //每页显示条数
                    userId: this.userInfo.id, //查询待办或已办时传 列表页查询不传
                    modelType: 3, //0-待办 1-已办 传2 表示列表页查询
                    startDates: data.startDates, //进场开始时间
                    endDates: data.endDates, //进场结束时间
                    timeDates: [data.startDates, data.endDates],
                };

            } else {
                this.getList.startDates = this.common.getDay(-90); // 开始时间
                this.getList.endDates = this.common.getDay(0); // 截至时间
                this.getList.timeDates = [this.getList.startDates, this.getList.endDates]
            }
            // this.yj_searchForm(this.getList);
        },

    },
};